<?php
/**
 *+------------------
 * Lflow
 *+------------------
 * Copyright (c) 2023~2030 gitee.com/liu_guan_qing All rights reserved.本版权不可删除，侵权必究
 *+------------------
 * Author: Mr.April(405784684@qq.com)
 *+------------------
 */

namespace app\adminapi\controller\v1\wf;

use app\adminapi\controller\AuthController;
use app\Request;
use app\services\system\admin\SystemAdminServices;
use lflow\core\services\DesignerServices;
use think\App;

class Designer extends AuthController
{

    protected DesignerServices $service;

    public function __construct(App $app, DesignerServices $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 资源列表
     *
     * @return \think\response\Json
     * @throws \ReflectionException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index(): \think\response\Json
    {
        $where = $this->request->getMore([
            ['id', ''],
            ['model_key', ''],
            ['model_name', ''],
            ['model_group_id', ''],
        ]);
        return app('json')->success($this->service->select($where));
    }

    /**
     * 获取指定资源
     *
     * @param string $id
     *
     * @return \think\Response\Json
     */
    public function read(string $id): \think\Response\Json
    {
        $ret = $this->service->find($id);
        if ($ret) {
            return app('json')->success($ret->toArray());
        } else {
            return app('json')->fail('参数错误');
        }
    }

    /**
     * 添加资源
     *
     * @param \app\Request $request
     *
     * @return \think\Response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(Request $request): \think\Response\Json
    {
        $data = $this->request->postMore([
            ['model_key', ''],
            ['model_name', ''],
            ['model_type', 10],
            ['model_group_id', '-1'],
            ['remarks', ''],
            ['user_id', $request->adminId()],
        ]);
        if (empty($data['model_key'])) {
            return app('json')->fail('模型编码不能为空');
        }
        if (empty($data['model_name'])) {
            return app('json')->fail('模型名称不能为空');
        }
        if ($this->service->save($data)) {
            return app('json')->success(100021);
        } else {
            return app('json')->fail(100022);
        }
    }

    /**
     * 流程定义保存
     *
     * @return \think\Response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function saveDefine(): \think\Response\Json
    {
        $data = $this->request->postMore([
            ['id', ''],
            ['content', '', '', 'data'],
        ]);
        if ($this->service->update($data['id'], ['variable' => $data['data']])) {
            return app('json')->success(100000);
        } else {
            return app('json')->fail(100006);
        }
    }

    /**
     * 删除指定资源
     *
     * @param string $id
     *
     * @return \think\Response\Json
     */
    public function delete(string $id): \think\Response\Json
    {
        if (!$id) return app('json')->fail(100100);
        if (!($this->service->del($id))) {
            return app('json')->fail(100008);
        }
        return app('json')->success('删除成功');
    }

    /**
     * 批量删除资源
     *
     * @return \think\Response\Json
     */
    public function batchRemove(): \think\Response\Json
    {
        $ids = $this->request->post('data');
        if (empty($ids)) return app('json')->fail(100100);
        if (!($this->service->batchUpdate(explode(',', $ids), ['delete_time' => time()]))) {
            return app('json')->fail(100008);
        }
        return app('json')->success('删除成功');
    }

    /**
     * 更新指定资源
     *
     * @param \app\Request $request
     * @param string       $id
     *
     * @return \think\Response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function update(Request $request, string $id): \think\Response\Json
    {
        if (!$id || !($this->service->get($id))) {
            return app('json')->fail(100026);
        }
        $data = $this->request->postMore([
            ['model_key', ''],
            ['model_name', ''],
            ['model_type', 10],
            ['model_group_id', '-1'],
            ['remarks', ''],
            ['user_id', $request->adminId()],
        ]);
        if ($this->service->update($id, $data)) {
            return app('json')->success(100001);
        } else {
            return app('json')->fail(100007);
        }
    }

    public function approveList(Request $request)
    {
        $data = $this->request->postMore([
            ['scope', ''],
            ['search', ''],
        ]);
        switch ((string)$data['scope']) {
            case '2':
                //指定分组
                break;
            case '3':
                //表单用户
                break;
            case '4':
                //直属主管
                break;
            case '5':
                //申请人
                break;
            case '1':
                //指定人员
            default:
                $systemAdminServices = app()->make(SystemAdminServices::class);
                $result              = $systemAdminServices->getAdminList(['account_like' => $data['search'] ?? '']);
                $data                = [];
                foreach ($result['list'] as $key => $value) {
                    $data['list'][$key]['name']  = $value['real_name'];
                    $data['list'][$key]['value'] = $value['account'];
                }
                $data['count'] = $result['count'];
                break;

        }
        return app('json')->success($data);
    }

    public function approveInfo(Request $request)
    {
        $data = $this->request->postMore([
            ['scope', ''],
            ['search', ''],
        ]);
        if (empty($data['scope'])) {
            return app('json')->success(['list' => [], 'count' => 0]);
        }
        if (empty($data['search'])) {
            return app('json')->success(['list' => [], 'count' => 0]);
        }
        switch ((string)$data['scope']) {
            case '2':
                //指定分组
                break;
            case '3':
                //表单用户
                break;
            case '4':
                //直属主管
                break;
            case '5':
                //申请人
                break;
            case '1':
                //指定人员
            default:
                $systemAdminServices = app()->make(SystemAdminServices::class);
                $result              = $systemAdminServices->getAdminList(['in_account' => $data['search'] ?? '']);
                $data                = [];
                foreach ($result['list'] as $key => $value) {
                    $data['list'][$key]['name']  = $value['real_name'];
                    $data['list'][$key]['value'] = $value['account'];
                }
                $data['count'] = $result['count'];
                break;
        }
        return app('json')->success($data);
    }

}
